package com.gopay.common.rcs.service.translimit.impl;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.constants.proccode.ProcCodeEnum;
import com.gopay.common.domain.rcs.RcsBAcctLimitStatistics;
import com.gopay.common.domain.rcs.RcsTransLimitStatistics;
import com.gopay.common.domain.rcs.req.RcsTransLimitRequest;
import com.gopay.common.domain.rcs.res.RcsTransLimitResponse;
import com.gopay.common.domain.solution.TranLimitRes;
import com.gopay.common.exception.GopayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 交易额度校验
 * 
 * @RcsTransLimitCheckService.java
 * @author liujy
 * @2013-7-22 下午1:47:48 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("rcsTransLimitFor00700Service")
public class RcsTransLimitFor00700ServiceImpl extends RcsTransLimitBaseServiceImpl {

    private Logger logger = LoggerFactory.getLogger(RcsTransLimitFor00700ServiceImpl.class);
    
    /**
     * 额度校验
     * 
     * @param request
     * @param response
     * @throws GopayException
     */
    @Override
    protected void checkLimit(RcsTransLimitRequest request, RcsTransLimitResponse response) throws GopayException {

        super.checkLimit(request, response);
        
        // 累计额度统计信息
        RcsTransLimitStatistics bankLimitStatistics = findRcsBAcctLimitStatistics(request);
        
        // 累计额度校验
        checkBankLimitStatistics(request, response, bankLimitStatistics);
    }

    /**
     * 
     * @param request
     * @param response
     * @param limitStatistics
     * @throws GopayException
     */
    private void checkBankLimitStatistics(RcsTransLimitRequest request, RcsTransLimitResponse response,
            RcsTransLimitStatistics limitStatistics)throws GopayException  {
        if (null == limitStatistics) {
            throw new GopayException(ProcCodeEnum.PROC_CODE_200F1002);
        }

        TranLimitRes limitConf = request.getCustTranLimitConf();
        BigDecimal totalAmt = request.getTotalAmt();

        // 单日累计额度
        if(limitConf.getSamePayeeLimit().compareTo(new BigDecimal("0.00")) >= 0){
            if (limitStatistics.getDayLimitStatcs().add(totalAmt).compareTo(limitConf.getSamePayeeLimit()) == 1) {
            	logger.error("同一收款人的同一银行卡单日累计额度超限：已发生累计金额="
						+ limitStatistics.getDayLimitStatcs() + ", 交易金额(批量总金额)="
						+ totalAmt + ", 同一收款人单日累计额度=" + limitConf.getSamePayeeLimit());
                throw new GopayException(ProcCodeEnum.PROC_CODE_200E5008);
            }
        }
    }
    
    /**
     * 
     * @param request
     * @return
     */
    private RcsTransLimitStatistics findRcsBAcctLimitStatistics(RcsTransLimitRequest request) {
        try {
            RcsBAcctLimitStatistics limitStatistics = rcsBAcctLimitStatisticsManager.getTransLimitStatistics(
                    request.getCustId(), request.getAcctProd(), request.getIntTxnCd(), request.getTranDirection(),
                    request.getTransDate(), request.getPbocBankCode(), request.getBankAcctNum(), request.getBankName(),
                    request.getBankAcctName());
            /**
             * modifiede by zyt 日志中明文卡号做掩码显示
             * 2017-2-14
             */
            logger.info("累计额度统计信息：" + LoggerMaskUtils.maskString(limitStatistics.toString()) + ", gopayOrderId: " +request.getGopayOrderId());
            //logger.info("累计额度统计信息：" + limitStatistics + ", gopayOrderId: " +request.getGopayOrderId());
            return limitStatistics;
        } catch (Exception e) {
            logger.error("获取客户累计额度统计信息异常：" + request, e);
        }
        return null;
    }
}